package com.zytlifeservice.controller.pay;

import com.qianmi.open.api.ApiException;
import com.zytlifeservice.domain.Result;
import com.zytlifeservice.exception.UserException;
import com.zytlifeservice.repository.pay.MobileRechargeOrderRepository;
import com.zytlifeservice.repository.pay.WxPayOrderRepository;
import com.zytlifeservice.service.pay.AlipayService;
import com.zytlifeservice.service.pay.MobileRechargeService;
import com.zytlifeservice.service.pay.WxPayService;
import com.zytlifeservice.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by MJS on 2017/5/26.
 */
@RestController
@RequestMapping(value = "/mobileRecharge")
public class MobileRechargeController {

    private static final Logger logger = LoggerFactory.getLogger(MobileRechargeController.class);

    @Autowired
    AlipayService alipayService;

    @Autowired
    WxPayOrderRepository wxPayOrderRepository;

    @Autowired
    WxPayService wxPayService;

    @Autowired
    MobileRechargeOrderRepository mobileRechargeOrderRepository;

    @Autowired
    MobileRechargeService mobileRechargeService;

    @PostMapping(value = "/preCreate")
    public Result preCreate(@RequestParam("mobileNo") String mobileNo,
                            @RequestParam("rechargeAmount") String rechargeAmount,
                            @RequestParam("userId") Long userId) {
        try {
            String[] result = mobileRechargeService.createOrder(mobileNo, rechargeAmount, userId);
            if ("success".equals(result[0])) {
                return ResultUtil.success(result[1], "预下单成功");
            } else {
                throw new UserException(result[1], 202);
            }
        } catch (ApiException e) {
            e.printStackTrace();
            throw new UserException("预下单失败", 202);
        }
    }


    @PostMapping(value = "/preCreateWxOrder")
    public Result preCreateWxOrder(@RequestParam("mobileNo") String mobileNo,
                                   @RequestParam("rechargeAmount") String rechargeAmount,
                                   @RequestParam("userId") Long userId,
                                   @RequestParam("ip") String ip) {

        try {
            String[] result = mobileRechargeService.createWxOrder(mobileNo, rechargeAmount, userId, ip);
            if ("success".equals(result[0])) {
                return ResultUtil.success(result[1], "预下单成功");
            } else {
                throw new UserException(result[1], 202);
            }
        } catch (ApiException e) {
            e.printStackTrace();
            throw new UserException("预下单失败", 202);
        }
    }

    @PostMapping(value = "/getPhoneItem")
    public Result queryPhoneItem() {
        return ResultUtil.success(mobileRechargeService.queryAll(), "查询成功");
    }

}
